package com.bizdk.fastfx.model;

import com.bizdk.fastfx.domain.entity.Patient;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.util.StringJoiner;

public class PatientModel {
    private StringProperty id = new SimpleStringProperty("");
    private StringProperty name = new SimpleStringProperty("");
    private StringProperty gender = new SimpleStringProperty("");
    private final ObservableList<ImageView> imageViews = FXCollections.observableArrayList();

    private boolean create;

    public PatientModel() {
    }

    public PatientModel(Patient patient) {
        setId(patient.getId());
        setName(patient.getName());
        setGender(patient.getGender());

        String images = patient.getImages();
        if(null != images) {
            ImageView imageView = null;
            String[] imageUriArr = images.split(",");
            for (String imageUri : imageUriArr) {
                if (!StringUtils.hasLength(imageUri)) {
                    continue;
                }
                try (FileInputStream fileInputStream = new FileInputStream(imageUri);){
                    Image image = new Image(fileInputStream);
                    imageView = new ImageView(image);
                    imageView.setUserData(imageUri);
                    imageView.setFitHeight(100);
                    imageView.setFitWidth(100);
                    imageViews.add(imageView);

                } catch (Exception ex) {
                }
            }
        }
    }

    public PatientModel(String id, String name, String gender, String doctor) {
        setId(id);
        setName(name);
        setGender(gender);
    }

    public String getId() {
        return id.get();
    }

    public void setId(String id) {
        this.id.set(id);
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getGender() {
        return gender.get();
    }

    public void setGender(String gender) {
        this.gender.set(gender);
    }

    public void addImageView(ImageView imageView) {
        imageViews.add(imageView);
    }

    public ObservableList<ImageView> getImageViews() {
        return imageViews;
    }

    public boolean isCreate() {
        return create;
    }

    public void setCreate(boolean create) {
        this.create = create;
    }

    public Patient toPatient() {
        Patient patient = new Patient();
        patient.setId(getId());
        patient.setName(getName());
        patient.setGender(getGender());

        if (null != imageViews && !imageViews.isEmpty()) {
            StringJoiner strJoi = new StringJoiner(",");
            for (ImageView imageView : imageViews) {
                strJoi.add(imageView.getUserData().toString());
            }
            patient.setImages(strJoi.toString());
        }
        return patient;
    }
}
